home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / misc / volume16 / sipp2.0 / part02 < prev    next >
Encoding:
Internet Message Format  |  1991-01-02  |  46.5 KB

  1. From: kent@sparky.IMD.Sterling.COM (Kent Landfield)
  2. Newsgroups: comp.sources.misc
  3. Subject: v16i006:  sipp 2.0 - a library for 3D graphics, Part02/06
  4. Message-ID: <1991Jan3.065629.5048@sparky.IMD.Sterling.COM>
  5. Date: 3 Jan 91 06:56:29 GMT
  6. Approved: kent@sparky.imd.sterling.com
  7. X-Checksum-Snefru: 2ca9bec1 dd5b86e4 e42e10dd dfe018dd
  8.  
  9. Submitted-by: ingwa@isy.liu.se (Inge Wallin)
  10. Posting-number: Volume 16, Issue 6
  11. Archive-name: sipp2.0/part02
  12.  
  13. #!/bin/sh
  14. # This is part 02 of sipp-2.0
  15. # ============= libsipp/bumpy.c ==============
  16. if test ! -d 'libsipp'; then
  17.     echo 'x - creating directory libsipp'
  18.     mkdir 'libsipp'
  19. fi
  20. if test -f 'libsipp/bumpy.c' -a X"$1" != X"-c"; then
  21.     echo 'x - skipping libsipp/bumpy.c (File already exists)'
  22. else
  23. echo 'x - extracting libsipp/bumpy.c (Text)'
  24. sed 's/^X//' << 'SHAR_EOF' > 'libsipp/bumpy.c' &&
  25. /*
  26. X * erode shader - simulates an eroded surface using noise
  27. X */
  28. X
  29. #include <math.h>
  30. #include <stdio.h>
  31. X
  32. #include "sipp.h"
  33. #include "geometric.h"
  34. #include "noise.h"
  35. #include "shaders.h"
  36. X
  37. X
  38. extern bool noise_ready;
  39. X
  40. X
  41. void
  42. bumpy_shader(nx, ny, nz, u, v, w, view_vec, lights, bd, color)
  43. X    double        nx, ny, nz;
  44. X    double        u, v, w;
  45. X    Vector        view_vec;
  46. X    Lightsource  *lights;
  47. X    Bumpy_desc   *bd;
  48. X    Color        *color;
  49. {
  50. X    Vector     tmp;
  51. X    double     len;
  52. X    double     no;
  53. X
  54. X    if (!noise_ready) {
  55. X        noise_init();
  56. X    }
  57. X
  58. X    tmp.x = u * bd->scale;
  59. X    tmp.y = v * bd->scale;
  60. X    tmp.z = w * bd->scale;
  61. X
  62. X    if ((bd->bumpflag && bd->holeflag)
  63. X          || ((no = noise(tmp)) < 0.0 && bd->bumpflag)
  64. X          || (no > 0.0 && bd->holeflag)) {
  65. X        tmp = Dnoise(tmp);
  66. X        len = sqrt(nx * nx + ny * ny + nz * nz);
  67. X        nx = nx / len + tmp.x;
  68. X        ny = ny / len + tmp.y;
  69. X        nz = nz / len + tmp.z;
  70. X    }
  71. X
  72. X    bd->shader(nx, ny, nz, u, v, w, view_vec, lights, bd->surface, color);
  73. }
  74. SHAR_EOF
  75. chmod 0644 libsipp/bumpy.c ||
  76. echo 'restore of libsipp/bumpy.c failed'
  77. Wc_c="`wc -c < 'libsipp/bumpy.c'`"
  78. test 1027 -eq "$Wc_c" ||
  79.     echo 'libsipp/bumpy.c: original size 1027, current size' "$Wc_c"
  80. fi
  81. # ============= libsipp/cylinder.c ==============
  82. if test -f 'libsipp/cylinder.c' -a X"$1" != X"-c"; then
  83.     echo 'x - skipping libsipp/cylinder.c (File already exists)'
  84. else
  85. echo 'x - extracting libsipp/cylinder.c (Text)'
  86. sed 's/^X//' << 'SHAR_EOF' > 'libsipp/cylinder.c' &&
  87. #include <xalloca.h>
  88. #include <math.h>
  89. X
  90. #include <sipp.h>
  91. X
  92. X
  93. Object *
  94. sipp_cylinder(radius, length, res, surface, shader)
  95. X    double  radius;
  96. X    double  length;
  97. X    int     res;
  98. X    void   *surface;
  99. X    Shader *shader;
  100. {
  101. X    Object *cylinder;
  102. X    double *x_arr;
  103. X    double *y_arr;
  104. X    int     i;
  105. X
  106. X    x_arr = (double *)alloca(res * sizeof(double));
  107. X    y_arr = (double *)alloca(res * sizeof(double));
  108. X
  109. X    for (i = 0; i < res; i++) {
  110. X        x_arr[i] = radius * cos(i * 2.0 * M_PI / res);
  111. X        y_arr[i] = radius * sin(i * 2.0 * M_PI / res);
  112. X    }
  113. X
  114. X    cylinder = object_create();
  115. X
  116. X    /* Create the outer surface */
  117. X    for (i = 0; i < res; i++) {
  118. X        vertex_tx_push(x_arr[i], y_arr[i], -length / 2.0, 
  119. X                       x_arr[i], y_arr[i], -length / 2.0);
  120. X        vertex_tx_push(x_arr[(i + 1) % res], y_arr[(i + 1) % res],
  121. X                       -length / 2.0, 
  122. X                       x_arr[(i + 1) % res], y_arr[(i + 1) % res],
  123. X                       -length / 2.0);
  124. X        vertex_tx_push(x_arr[(i + 1) % res], y_arr[(i + 1) % res],
  125. X                       length / 2.0, 
  126. X                       x_arr[(i + 1) % res], y_arr[(i + 1) % res],
  127. X                       length / 2.0);
  128. X        vertex_tx_push(x_arr[i], y_arr[i], length / 2.0, 
  129. X                       x_arr[i], y_arr[i], length / 2.0);
  130. X        polygon_push();
  131. X    }
  132. X    object_add_surface(cylinder, surface_create(surface, shader));
  133. X
  134. X    /* Create the bottom lid */
  135. X    for (i = res - 1; i >= 0; i--) {
  136. X        vertex_tx_push(x_arr[i], y_arr[i], -length / 2.0, 
  137. X                       x_arr[i], y_arr[i], -length / 2.0);
  138. X    }
  139. X    polygon_push();
  140. X    object_add_surface(cylinder, surface_create(surface, shader));
  141. X
  142. X    /* Create the top lid. */
  143. X    for (i = 0; i < res; i++) {
  144. X        vertex_tx_push(x_arr[i], y_arr[i], length / 2.0, 
  145. X                       x_arr[i], y_arr[i], length / 2.0);
  146. X    }
  147. X    polygon_push();
  148. X    object_add_surface(cylinder, surface_create(surface, shader));
  149. X
  150. X    return cylinder;
  151. }
  152. SHAR_EOF
  153. chmod 0644 libsipp/cylinder.c ||
  154. echo 'restore of libsipp/cylinder.c failed'
  155. Wc_c="`wc -c < 'libsipp/cylinder.c'`"
  156. test 1981 -eq "$Wc_c" ||
  157.     echo 'libsipp/cylinder.c: original size 1981, current size' "$Wc_c"
  158. fi
  159. # ============= libsipp/ellipsoid.c ==============
  160. if test -f 'libsipp/ellipsoid.c' -a X"$1" != X"-c"; then
  161.     echo 'x - skipping libsipp/ellipsoid.c (File already exists)'
  162. else
  163. echo 'x - extracting libsipp/ellipsoid.c (Text)'
  164. sed 's/^X//' << 'SHAR_EOF' > 'libsipp/ellipsoid.c' &&
  165. #include <xalloca.h>
  166. #include <math.h>
  167. X
  168. #include <sipp.h>
  169. X
  170. X
  171. Object *
  172. sipp_ellipsoid(x_rad, y_rad, z_rad, res, surface, shader)
  173. X    double  x_rad;
  174. X    double  y_rad;
  175. X    double  z_rad;
  176. X    int     res;
  177. X    void   *surface;
  178. X    Shader *shader;
  179. {
  180. X    int      i, j;
  181. X    double   factor;
  182. X    double   factor1;
  183. X    double   factor2;
  184. X    double   zradprim;
  185. X    double   zradprim1;
  186. X    double   zradprim2;
  187. X    double  *x_arr;
  188. X    double  *y_arr;
  189. X    Object  *ellipsoid;
  190. X    
  191. X    /* Odd resolutions make ugly spheres since the poles will be */
  192. X    /* different in size. */
  193. X    if (res & 1) {
  194. X        res++;
  195. X    }
  196. X
  197. X    /* Create two arrays with the coordinates of the points */
  198. X    /* around the perimeter at Z = 0 */
  199. X    x_arr = (double *) alloca(res * sizeof(double));
  200. X    y_arr = (double *) alloca(res * sizeof(double));
  201. X    for (i = 0; i < res; i++) {
  202. X        x_arr[i] = x_rad * cos(i * 2.0 * M_PI / res);
  203. X        y_arr[i] = y_rad * sin(i * 2.0 * M_PI / res);
  204. X    }
  205. X
  206. X    /* Create the top pole */
  207. X    factor = sin(2.0 * M_PI / res);
  208. X    zradprim = z_rad * cos(2.0 * M_PI / res);
  209. X    for (i = 0; i < res; i++) {
  210. X        vertex_tx_push(x_arr[i] * factor, y_arr[i] * factor, zradprim, 
  211. X                       x_arr[i] * factor, y_arr[i] * factor, zradprim);
  212. X        vertex_tx_push(factor * x_arr[(i + 1) % res],
  213. X                       factor * y_arr[(i + 1) % res],
  214. X                       zradprim, 
  215. X                       factor * x_arr[(i + 1) % res],
  216. X                       factor * y_arr[(i + 1) % res],
  217. X                       zradprim);
  218. X        vertex_tx_push(0.0, 0.0, z_rad, 0.0, 0.0, z_rad);
  219. X        polygon_push();
  220. X    }
  221. X
  222. X    /* Create the surface between the poles. */
  223. X    factor2 = factor;
  224. X    zradprim2 = zradprim;
  225. X    for (j = 1; j < res / 2 - 1; j++) {
  226. X        factor1 = factor2;
  227. X        factor2 = sin((j + 1) * M_PI / (res / 2));
  228. X        zradprim1 = zradprim2;
  229. X        zradprim2 = z_rad * cos((j + 1) * M_PI / (res / 2));
  230. X
  231. X        for (i = 0; i < res; i++) {
  232. X            vertex_tx_push(factor1 * x_arr[i], factor1 * y_arr[i], zradprim1, 
  233. X                           factor1 * x_arr[i], factor1 * y_arr[i], zradprim1);
  234. X            vertex_tx_push(factor2 * x_arr[i], factor2 * y_arr[i], zradprim2, 
  235. X                           factor2 * x_arr[i], factor2 * y_arr[i], zradprim2);
  236. X            vertex_tx_push(factor2 * x_arr[(i + 1) % res], 
  237. X                           factor2 * y_arr[(i + 1) % res], zradprim2, 
  238. X                           factor2 * x_arr[(i + 1) % res],
  239. X                           factor2 * y_arr[(i + 1) % res], zradprim2);
  240. X            vertex_tx_push(factor1 * x_arr[(i + 1) % res], 
  241. X                           factor1 * y_arr[(i + 1) % res], zradprim1, 
  242. X                           factor1 * x_arr[(i + 1) % res],
  243. X                           factor1 * y_arr[(i + 1) % res], zradprim1);
  244. X            polygon_push();
  245. X        }
  246. X    }
  247. X
  248. X    /* Create the bottom pole */
  249. X    factor = sin(2.0 * M_PI / res);
  250. X    zradprim = -z_rad * cos(2.0 * M_PI / res);
  251. X    for (i = 0; i < res; i++) {
  252. X        vertex_tx_push(x_arr[(i + 1) % res] * factor,
  253. X                       y_arr[(i + 1) % res] * factor,
  254. X                       zradprim, 
  255. X                       x_arr[(i + 1) % res] * factor,
  256. X                       y_arr[(i + 1) % res] * factor,
  257. X                       zradprim);
  258. X        vertex_tx_push(x_arr[i] * factor, y_arr[i] * factor, zradprim, 
  259. X                       x_arr[i] * factor, y_arr[i] * factor, zradprim);
  260. X        vertex_tx_push(0.0, 0.0, -z_rad, 0.0, 0.0, -z_rad);
  261. X        polygon_push();
  262. X    }
  263. X
  264. X    ellipsoid = object_create();
  265. X    object_add_surface(ellipsoid, surface_create(surface, shader));
  266. X
  267. X    return ellipsoid;
  268. }
  269. X
  270. X
  271. Object *
  272. sipp_sphere(radius, res, surface, shader)
  273. X    double  radius;
  274. X    int     res;
  275. X    void   *surface;
  276. X    Shader *shader;
  277. {
  278. X    return sipp_ellipsoid(radius, radius, radius, res, surface, shader);
  279. }
  280. SHAR_EOF
  281. chmod 0644 libsipp/ellipsoid.c ||
  282. echo 'restore of libsipp/ellipsoid.c failed'
  283. Wc_c="`wc -c < 'libsipp/ellipsoid.c'`"
  284. test 3850 -eq "$Wc_c" ||
  285.     echo 'libsipp/ellipsoid.c: original size 3850, current size' "$Wc_c"
  286. fi
  287. # ============= libsipp/geometric.c ==============
  288. if test -f 'libsipp/geometric.c' -a X"$1" != X"-c"; then
  289.     echo 'x - skipping libsipp/geometric.c (File already exists)'
  290. else
  291. echo 'x - extracting libsipp/geometric.c (Text)'
  292. sed 's/^X//' << 'SHAR_EOF' > 'libsipp/geometric.c' &&
  293. /*
  294. X * File: geometric.c
  295. X *
  296. X * Matrixes, transformations and coordinates.
  297. X */
  298. X
  299. #include <stdio.h>
  300. #include <math.h>
  301. #include "sipp.h"
  302. #include "geometric.h"
  303. X
  304. X
  305. X
  306. /* =================================================================== */
  307. X
  308. /*
  309. X * Allocate a new matrix, and if INITMAT != NULL copy the contents
  310. X * of INITMAT to the new matrix.
  311. X */
  312. X
  313. Transf_mat *
  314. new_matrix(initmat)
  315. X    Transf_mat  * initmat;
  316. {
  317. X    Transf_mat  * mat;
  318. X
  319. X    mat = (Transf_mat *) malloc(sizeof(Transf_mat));
  320. X    if (initmat != NULL)
  321. X    MatCopy(mat, initmat);
  322. X
  323. X    return mat;
  324. }
  325. X
  326. X
  327. /* =================================================================== */
  328. /*          Transformation routines (see also geometric.h)             */
  329. X
  330. X
  331. /*
  332. X * Normalize a vector.
  333. X */
  334. X
  335. void
  336. vecnorm(vec)
  337. X    Vector  *vec;
  338. {
  339. X    double   len;
  340. X
  341. X    len =  VecLen(*vec);
  342. X    if (len == 0.0)
  343. X    fprintf(stderr, "vecnorm(): Vector has length 0!\n");
  344. X    else
  345. X    VecScalMul(*vec, 1.0 / len, *vec);
  346. }
  347. X
  348. X
  349. X
  350. /*
  351. X * Set MAT to the transformation matrix that represents the 
  352. X * concatenation between the previous transformation in MAT
  353. X * and a translation along the Vector VEC.
  354. X *
  355. X * [a  b  c  0]   [ 1  0  0  0]     [ a     b     c    0]
  356. X * [d  e  f  0]   [ 0  1  0  0]     [ d     e     f    0]
  357. X * [g  h  i  0] * [ 0  0  1  0]  =  [ g     h     i    0]
  358. X * [j  k  l  1]   [Tx Ty Tz  1]     [j+Tx  k+Ty  l+Tz  1]
  359. X */
  360. X
  361. void
  362. mat_translate(mat,  dx,  dy,  dz)
  363. X    Transf_mat  * mat;
  364. X    double        dx;
  365. X    double        dy; 
  366. X    double        dz;
  367. {
  368. X    mat->mat[3][0] += dx;
  369. X    mat->mat[3][1] += dy;
  370. X    mat->mat[3][2] += dz;
  371. }
  372. X
  373. X
  374. X
  375. /*
  376. X * Set MAT to the transformation matrix that represents the 
  377. X * concatenation between the previous transformation in MAT
  378. X * and a rotation with the angle ANG around the X axis.
  379. X *
  380. X * [a  b  c  0]   [1   0   0  0]     [a  b*Ca-c*Sa  b*Sa+c*Ca  0]
  381. X * [d  e  f  0]   [0  Ca  Sa  0]     [d  e*Ca-f*Sa  e*Sa+f*Ca  0]
  382. X * [g  h  i  0] * [0 -Sa  Ca  0]  =  [g  h*Ca-i*Sa  h*Sa+i*Ca  0]
  383. X * [j  k  l  1]   [0   0   0  1]     [j  k*Ca-l*Sa  k*Se+l*Ca  1]
  384. X */
  385. X
  386. void
  387. mat_rotate_x(mat, ang)
  388. X    Transf_mat  * mat;
  389. X    double        ang;
  390. {
  391. X    double   cosang;
  392. X    double   sinang;
  393. X    double   tmp;
  394. X    int      i;
  395. X    
  396. X    cosang = cos(ang);
  397. X    sinang = sin(ang);
  398. X    for (i = 0; i < 4; ++i) {
  399. X    tmp = mat->mat[i][1];
  400. X    mat->mat[i][1] = mat->mat[i][1] * cosang
  401. X                   - mat->mat[i][2] * sinang;
  402. X    mat->mat[i][2] = tmp * sinang + mat->mat[i][2] * cosang;
  403. X    }
  404. }
  405. X
  406. X
  407. X
  408. /*
  409. X * Set MAT to the transformation matrix that represents the 
  410. X * concatenation between the previous transformation in MAT
  411. X * and a rotation with the angle ANG around the Y axis.
  412. X *
  413. X * [a  b  c  0]   [Ca   0 -Sa  0]     [a*Ca+c*Sa  b  -a*Sa+c*Ca  0]
  414. X * [d  e  f  0] * [ 0   1   0  0]  =  [d*Ca+f*Sa  e  -d*Sa+f*Ca  0]
  415. X * [g  h  i  0]   [Sa   0  Ca  0]     [g*Ca+i*Sa  h  -g*Sa+i*Ca  0]
  416. X * [j  k  l  1]   [ 0   0   0  1]     [j*Ca+l*Sa  k  -j*Sa+l*Ca  1]
  417. X */
  418. X
  419. void
  420. mat_rotate_y(mat, ang)
  421. X    Transf_mat  * mat;
  422. X    double        ang;
  423. {
  424. X    double   cosang;
  425. X    double   sinang;
  426. X    double   tmp;
  427. X    int      i;
  428. X    
  429. X    cosang = cos(ang);
  430. X    sinang = sin(ang);
  431. X    for (i = 0; i < 4; ++i) {
  432. X    tmp = mat->mat[i][0];
  433. X    mat->mat[i][0] = mat->mat[i][0] * cosang
  434. X                   + mat->mat[i][2] * sinang;
  435. X    mat->mat[i][2] = -tmp * sinang + mat->mat[i][2] * cosang;
  436. X    }
  437. }
  438. X
  439. X
  440. X
  441. /*
  442. X * Set MAT to the transformation matrix that represents the 
  443. X * concatenation between the previous transformation in MAT
  444. X * and a rotation with the angle ANG around the Z axis.
  445. X *
  446. X * [a  b  c  0]   [ Ca  Sa   0  0]     [a*Ca-b*Sa  a*Sa+b*Ca  c  0]
  447. X * [d  e  f  0]   [-Sa  Ca   0  0]     [d*Ca-e*Sa  d*Sa+e*Ca  f  0]
  448. X * [g  h  i  0] * [  0   0   1  0]  =  [g*Ca-h*Sa  g*Sa+h*Ca  i  0]
  449. X * [j  k  l  1]   [  0   0   0  1]     [j*Ca-k*Sa  j*Sa+k*Ca  l  0]
  450. X */
  451. X
  452. void
  453. mat_rotate_z(mat, ang)
  454. X    Transf_mat  * mat;
  455. X    double        ang;
  456. {
  457. X    double   cosang;
  458. X    double   sinang;
  459. X    double   tmp;
  460. X    int      i;
  461. X    
  462. X    cosang = cos(ang);
  463. X    sinang = sin(ang);
  464. X    for (i = 0; i < 4; ++i) {
  465. X    tmp = mat->mat[i][0];
  466. X    mat->mat[i][0] = mat->mat[i][0] * cosang
  467. X                   - mat->mat[i][1] * sinang;
  468. X    mat->mat[i][1] = tmp * sinang + mat->mat[i][1] * cosang;
  469. X    }
  470. }
  471. X
  472. X
  473. X
  474. /*
  475. X * Set MAT to the transformation matrix that represents the
  476. X * concatenation between the previous transformation in MAT
  477. X * and a rotation with the angle ANG around the line represented
  478. X * by the point POINT and the vector VECTOR.
  479. X */
  480. X
  481. void
  482. mat_rotate(mat, point, vector, ang)
  483. X    Transf_mat  * mat;
  484. X    Vector      * point;
  485. X    Vector      * vector;
  486. X    double        ang;
  487. {
  488. X    double   ang2;
  489. X    double   ang3;
  490. X
  491. X    ang2 = atan2(vector->y, vector->x);
  492. X    ang3 = atan2(hypot(vector->x, vector->y), vector->z);
  493. X    mat_translate(mat, -point->x, -point->y, -point->z);
  494. X    mat_rotate_z(mat, -ang2);
  495. X    mat_rotate_y(mat, -ang3);
  496. X    mat_rotate_z(mat, ang);
  497. X    mat_rotate_z(mat, ang2);
  498. X    mat_rotate_y(mat, ang3);
  499. X    mat_translate(mat, point->x, point->y, point->z);
  500. }
  501. X
  502. X
  503. X
  504. /*
  505. X * Set MAT to the transformation matrix that represents the 
  506. X * concatenation between the previous transformation in MAT
  507. X * and a scaling with the scaling factors in the Vector scale.
  508. X *
  509. X * [a  b  c  0]   [Sx  0  0  0]     [a*Sx  b*Sy  c*Sz  0]
  510. X * [d  e  f  0]   [ 0 Sy  0  0]     [d*Sx  e*Sy  f*Sz  0]
  511. X * [g  h  i  0] * [ 0  0 Sz  0]  =  [g*Sx  h*Sy  i*Sz  0]
  512. X * [j  k  l  1]   [ 0  0  0  1]     [j*Sx  k*Sy  l*Sz  1]
  513. X */
  514. X
  515. void
  516. mat_scale(mat, xscale, yscale, zscale)
  517. X    Transf_mat  * mat;
  518. X    double        xscale;
  519. X    double        yscale;
  520. X    double        zscale;
  521. {
  522. X    int   i;
  523. X
  524. X    for (i = 0; i < 4; ++i) {
  525. X    mat->mat[i][0] *= xscale;
  526. X    mat->mat[i][1] *= yscale;
  527. X    mat->mat[i][2] *= zscale;
  528. X    }
  529. }
  530. X
  531. X
  532. X
  533. /*
  534. X * Set MAT to the transformation matrix that represents the
  535. X * concatenation between the previous transformation in MAT
  536. X * and a mirroring in the plane defined by the point POINT
  537. X * and the normal vector NORMAL.
  538. X */
  539. X
  540. void
  541. mat_mirror_plane(mat, point, norm)
  542. X    Transf_mat  * mat;
  543. X    Vector      * point;
  544. X    Vector      * norm;
  545. {
  546. X    Transf_mat   tmp;
  547. X    double   factor;
  548. X
  549. X    /* The first thing we do is to make a transformation matrix */
  550. X    /* for mirroring through a plane with the same normal vector */
  551. X    /* as our, but through the origin instead. */
  552. X    factor = 2.0 / (norm->x * norm->x + norm->y * norm->y 
  553. X            + norm->z * norm->z);
  554. X    
  555. X    /* The diagonal elements. */
  556. X    tmp.mat[0][0] = 1 - factor * norm->x * norm->x;
  557. X    tmp.mat[1][1] = 1 - factor * norm->y * norm->y;
  558. X    tmp.mat[2][2] = 1 - factor * norm->z * norm->z;
  559. X    
  560. X    /* The rest of the matrix */
  561. X    tmp.mat[1][0] = tmp.mat[0][1] = -factor * norm->x * norm->y;
  562. X    tmp.mat[2][0] = tmp.mat[0][2] = -factor * norm->x * norm->z;
  563. X    tmp.mat[2][1] = tmp.mat[1][2] = -factor * norm->y * norm->z;
  564. X    tmp.mat[3][0] = tmp.mat[3][1] = tmp.mat[3][2] = 0.0;
  565. X
  566. X    /* Do the actual transformation. This is done in 3 steps: */
  567. X    /* 1) Translate the plane so that it goes through the origin. */
  568. X    /* 2) Do the actual mirroring. */
  569. X    /* 3) Translate it all back to the starting position. */
  570. X    mat_translate(mat, -point->x, -point->y, -point->z);
  571. X    mat_mul(mat, mat, &tmp);
  572. X    mat_translate(mat, point->x, point->y, point->z);
  573. }
  574. X
  575. X
  576. X
  577. /*
  578. X * Multiply the Matrix A with the Matrix B, and store the result
  579. X * into the Matrix RES. It is possible for RES to point to the 
  580. X * same Matrix as either A or B since the result is stored into
  581. X * a temporary during computation.
  582. X *
  583. X * [a b c 0]  [A B C 0]     [aA+bD+cG    aB+bE+cH    aC+bF+cI    0]
  584. X * [d e f 0]  [D E F 0]     [dA+eD+fG    dB+eE+fH    dC+eF+fI    0]
  585. X * [g h i 0]  [G H I 0]  =  [gA+hD+iG    gB+hE+iH    gC+hF+iI    0]
  586. X * [j k l 1]  [J K L 1]     [jA+kD+lG+J  jB+kE+lH+K  jC+kF+lI+L  1]
  587. X */
  588. X
  589. void
  590. mat_mul(res, a, b)
  591. X    Transf_mat  * res;
  592. X    Transf_mat  * a;
  593. X    Transf_mat  * b;
  594. {
  595. X    Transf_mat   tmp;
  596. X    int      i;
  597. X
  598. X    for (i = 0; i < 4; ++i) {
  599. X    tmp.mat[i][0] = a->mat[i][0] * b->mat[0][0]
  600. X                  + a->mat[i][1] * b->mat[1][0] 
  601. X                  + a->mat[i][2] * b->mat[2][0];
  602. X    tmp.mat[i][1] = a->mat[i][0] * b->mat[0][1]
  603. X                    + a->mat[i][1] * b->mat[1][1] 
  604. X                  + a->mat[i][2] * b->mat[2][1];
  605. X    tmp.mat[i][2] = a->mat[i][0] * b->mat[0][2]
  606. X                   + a->mat[i][1] * b->mat[1][2]
  607. X                  + a->mat[i][2] * b->mat[2][2];
  608. X    }
  609. X
  610. X    tmp.mat[3][0] += b->mat[3][0];
  611. X    tmp.mat[3][1] += b->mat[3][1];
  612. X    tmp.mat[3][2] += b->mat[3][2];
  613. X
  614. X    MatCopy(res, &tmp);
  615. }
  616. X
  617. X
  618. X
  619. /*
  620. X * Transform the Point3d VEC with the transformation matrix MAT, and
  621. X * put the result into the vector *RES.
  622. X *
  623. X *               [a  b  c  0]
  624. X *               [d  e  f  0]
  625. X * [x  y  z  1]  [g  h  i  0]  =  [ax+dy+gz+j  bx+ey+hz+k  cx+fy+iz+l  1]
  626. X *               [j  k  l  1]
  627. X */
  628. X
  629. void
  630. point_transform(res, vec, mat)
  631. X    Vector      * res;
  632. X    Vector      * vec;
  633. X    Transf_mat  * mat;
  634. {
  635. X    res->x = mat->mat[0][0] * vec->x + mat->mat[1][0] * vec->y 
  636. X        + mat->mat[2][0] * vec->z + mat->mat[3][0];
  637. X    res->y = mat->mat[0][1] * vec->x + mat->mat[1][1] * vec->y 
  638. X        + mat->mat[2][1] * vec->z + mat->mat[3][1];
  639. X    res->z = mat->mat[0][2] * vec->x + mat->mat[1][2] * vec->y 
  640. X        + mat->mat[2][2] * vec->z + mat->mat[3][2];
  641. }
  642. X
  643. X
  644. /*
  645. X * Multiply the 4x3 matrix MAT3 by the 4x4 matrix MAT4, putting the
  646. X * result in the 4x4 matrix RES.
  647. X */
  648. X
  649. void
  650. mat_mul34(res, mat3, mat4)
  651. X    double        res[4][4];
  652. X    Transf_mat  * mat3;
  653. X    double        mat4[4][4];
  654. {
  655. X    double tmp[4][4];
  656. X    int    row;
  657. X    int    col;
  658. X    int    k;
  659. X
  660. X    for (row = 0; row < 4; ++row) {
  661. X        for (col = 0; col < 4; ++col) {
  662. X            if (row == 3) {
  663. X                tmp[row][col] = mat4[3][col];
  664. X            } else {
  665. X                tmp[row][col] = 0.0;
  666. X            }
  667. X
  668. X            for (k = 0; k < 3; ++k) {
  669. X                tmp[row][col] += mat3->mat[row][k] * mat4[k][col];
  670. X            }
  671. X        }
  672. X    }
  673. X    memcpy(res, tmp, 4 * 4 * sizeof(double));
  674. }
  675. SHAR_EOF
  676. chmod 0644 libsipp/geometric.c ||
  677. echo 'restore of libsipp/geometric.c failed'
  678. Wc_c="`wc -c < 'libsipp/geometric.c'`"
  679. test 9833 -eq "$Wc_c" ||
  680.     echo 'libsipp/geometric.c: original size 9833, current size' "$Wc_c"
  681. fi
  682. # ============= libsipp/geometric.h ==============
  683. if test -f 'libsipp/geometric.h' -a X"$1" != X"-c"; then
  684.     echo 'x - skipping libsipp/geometric.h (File already exists)'
  685. else
  686. echo 'x - extracting libsipp/geometric.h (Text)'
  687. sed 's/^X//' << 'SHAR_EOF' > 'libsipp/geometric.h' &&
  688. /*
  689. X * File: geometric.h
  690. X *
  691. X * All kinds of stuff with matrixes, transformations, coordinates
  692. X * a.s.o.
  693. X */
  694. X
  695. /* Make sure no multiple including */
  696. #ifndef _GEOMETRIC_H_
  697. #define _GEOMETRIC_H_
  698. X
  699. #include <math.h>
  700. #ifndef NOMEMCPY
  701. #include <memory.h>
  702. #endif
  703. X
  704. X
  705. /* #define PI    3.1415926535897932384626 */
  706. X
  707. X
  708. /*
  709. X * Define a homogenous transformation matrix. The first row (vector) 
  710. X * is the new X axis, i.e. the X axis in the transformed coordinate 
  711. X * system. The second row is the new Y axis, and so on. The last row
  712. X * is the translation, for a transformed point.
  713. X *
  714. X * The reason we make surround the rows with a struct is that we
  715. X * don't want to say (Transf_mat *) &foo[0] instead of &foo when
  716. X * sending an address to a matrix as a parameter to a function.
  717. X * Alas, arrays are not first class objects in C.
  718. X */
  719. X
  720. X
  721. X
  722. /*
  723. X * NOTE:
  724. X * Capitalized types denote Vectors and other aggregates.
  725. X * Lower case denote scalars.
  726. X */
  727. X
  728. /* P = Pointd(x, y) */
  729. #define MakePoint2d(P, xx, yy)     { (P)[0]=(xx); \
  730. X                                   (P)[1]=(yy); }
  731. X
  732. /* P = Point3d(x, y, z) */
  733. #define MakePoint3d(P, xx, yy, zz) { (P)[0]=(xx); \
  734. X                                     (P)[1]=(yy); \
  735. X                                     (P)[2]=(zz); }
  736. X
  737. /* V = Vec(x, y, z) */
  738. #define MakeVector(V, xx, yy, zz)  { (V).x=(xx); \
  739. X                                     (V).y=(yy); \
  740. X                                     (V).z=(zz); }
  741. X
  742. /* A = -A */
  743. #define VecNegate(A)             { (A).x=0-(A).x; \
  744. X                       (A).y=0-(A).y; \
  745. X                       (A).z=0-(A).z; }
  746. X
  747. /* return A . B */
  748. #define VecDot(A, B)    ((A).x*(B).x+(A).y*(B).y+(A).z*(B).z)
  749. X
  750. /* return length(A) */
  751. #define VecLen(A)    (sqrt((double)VecDot(A, A)))
  752. X
  753. /* B = A */
  754. #define VecCopy(B, A)    ((B) = (A))
  755. X
  756. /* C = A + B */
  757. #define VecAdd(C, A, B)     { (C).x=(A).x+(B).x; \
  758. X               (C).y=(A).y+(B).y; \
  759. X               (C).z=(A).z+(B).z; }
  760. X
  761. /* C = A - B */
  762. #define VecSub(C, A, B)     { (C).x=(A).x-(B).x; \
  763. X               (C).y=(A).y-(B).y; \
  764. X               (C).z=(A).z-(B).z; }
  765. X
  766. /* C = a*A */
  767. #define VecScalMul(C, a, A)     { (C).x=(a)*(A).x; \
  768. X                   (C).y=(a)*(A).y; \
  769. X                   (C).z=(a)*(A).z; }
  770. X
  771. /* C = a*A + B */
  772. #define VecAddS(C, a, A, B)     { (C).x=(a)*(A).x+(B).x; \
  773. X                   (C).y=(a)*(A).y+(B).y; \
  774. X                   (C).z=(a)*(A).z+(B).z; }
  775. X
  776. /* C = a*A + b*B */
  777. #define VecComb(C, a, A, b, B)     { (C).x=(a)*(A).x+(b)*(B).x; \
  778. X                   (C).y=(a)*(A).y+(b)*(B).y; \
  779. X                    (C).z=(a)*(A).z+(b)*(B).z; }
  780. X
  781. /* C = A X B */
  782. #define VecCross(C, A, B)        { (C).x=(A).y*(B).z-(A).z*(B).y; \
  783. X                                   (C).y=(A).z*(B).x-(A).x*(B).z; \
  784. X                       (C).z=(A).x*(B).y-(A).y*(B).x; }
  785. X
  786. X
  787. /*-------------------------- Matrix operations -------------------------*/
  788. X
  789. X
  790. /* *A = *B    N.b. A and B are pointers! */
  791. #define MatCopy(A, B)         (*A) = (*B)
  792. X
  793. X
  794. /*--------------------- Transf_mat-Vector operations -------------------*/
  795. X
  796. X
  797. /* Store a Vector into a row in a Matrix */
  798. /* NOTE: This implementation depends on that a Vector is the same */
  799. /*       type as a row in a Matrix!! */
  800. #define Vec2Row(mat, row, vec)    ((mat).rows[row] = vec)
  801. X
  802. X
  803. /*----------------------------------------------------------------------*/
  804. X
  805. X
  806. /* Function declarations for the functions in geometric.c */
  807. X
  808. extern void          vecnorm(/* Vector */); /* Normalize a vector */
  809. extern Transf_mat  * new_matrix(/* Matrix * */);
  810. extern void          mat_translate(/* Matrix *, double, double, double */);
  811. extern void          mat_rotate_x(/* Matrix *, double */);
  812. extern void          mat_rotate_y(/* Matrix *, double */);
  813. extern void          mat_rotate_z(/* Matrix *, double */);
  814. extern void          mat_rotate(/* Matrix *, Point3d *, Vector *, double */);
  815. extern void          mat_scale(/* Matrix *, double, double, double */);
  816. extern void          mat_mirror_plane(/* Matrix *, Point3d *, Vector * */);
  817. extern void          mat_mul(/* Matrix *, Matrix *, Matrix * */);
  818. extern void          mat_mul34(/* double[4][4] *, Matrix *, double[4][4] */);
  819. extern void          point_transform(/* Point3d *, Point3d *, Matrix * */);
  820. X
  821. X
  822. #endif  /* _GEOMETRIC_H_ */
  823. SHAR_EOF
  824. chmod 0644 libsipp/geometric.h ||
  825. echo 'restore of libsipp/geometric.h failed'
  826. Wc_c="`wc -c < 'libsipp/geometric.h'`"
  827. test 4007 -eq "$Wc_c" ||
  828.     echo 'libsipp/geometric.h: original size 4007, current size' "$Wc_c"
  829. fi
  830. # ============= libsipp/granite.c ==============
  831. if test -f 'libsipp/granite.c' -a X"$1" != X"-c"; then
  832.     echo 'x - skipping libsipp/granite.c (File already exists)'
  833. else
  834. echo 'x - extracting libsipp/granite.c (Text)'
  835. sed 's/^X//' << 'SHAR_EOF' > 'libsipp/granite.c' &&
  836. /*
  837. X * Granite - using a 1/f fractal noise function for color values.  
  838. X */
  839. X
  840. #include <math.h>
  841. #include <stdio.h>
  842. X
  843. #include "sipp.h"
  844. #include "noise.h"
  845. #include "shaders.h"
  846. X
  847. X
  848. extern bool noise_ready;
  849. X
  850. X
  851. static void 
  852. granite (p, color, gd)
  853. X    Vector *p;
  854. X    Color *color;
  855. X    Granite_desc *gd;
  856. {
  857. X    int i;
  858. X    Vector v;
  859. X    double temp, n = 0.5, freq = 1.0;
  860. X
  861. X    v = *p;
  862. X    
  863. X    for (i = 0; i < 6 ; freq *= 2.0, i++) {
  864. X        v.x *= 4.0 * freq;
  865. X        v.y *= 4.0 * freq;
  866. X        v.z *= 4.0 * freq;
  867. X        temp = 0.5 * noise(&v);
  868. /*        temp = fabs(temp);*/
  869. X        n += temp / freq;
  870. X    }
  871. X
  872. X   color->red = gd->col1.red * n + gd->col2.red * (1.0 - n);
  873. X   color->grn = gd->col1.grn * n + gd->col2.grn * (1.0 - n);
  874. X   color->blu = gd->col1.blu * n + gd->col2.blu * (1.0 - n);
  875. }
  876. X
  877. X
  878. void
  879. granite_shader(nx, ny, nz, u, v, w, view_vec, lights, gd, color)
  880. X    double  nx, ny, nz, u, v, w;
  881. X    Vector  view_vec;
  882. X    Lightsource *lights;
  883. X    Granite_desc *gd;
  884. X    Color *color;
  885. {
  886. X    Vector     tmp;
  887. X    Surf_desc  surface;
  888. X
  889. X    if (!noise_ready) {
  890. X        noise_init();
  891. X    }
  892. X
  893. X    tmp.x = u * gd->scale;
  894. X    tmp.y = v * gd->scale;
  895. X    tmp.z = w * gd->scale;
  896. X    granite(&tmp, &surface.color, gd);
  897. X    surface.ambient  = gd->ambient;
  898. X    surface.specular = gd->specular;
  899. X    surface.c3       = gd->c3;
  900. X    basic_shader(nx, ny, nz, u, v, w, view_vec, lights, &surface, color);
  901. }
  902. SHAR_EOF
  903. chmod 0644 libsipp/granite.c ||
  904. echo 'restore of libsipp/granite.c failed'
  905. Wc_c="`wc -c < 'libsipp/granite.c'`"
  906. test 1373 -eq "$Wc_c" ||
  907.     echo 'libsipp/granite.c: original size 1373, current size' "$Wc_c"
  908. fi
  909. # ============= libsipp/marble.c ==============
  910. if test -f 'libsipp/marble.c' -a X"$1" != X"-c"; then
  911.     echo 'x - skipping libsipp/marble.c (File already exists)'
  912. else
  913. echo 'x - extracting libsipp/marble.c (Text)'
  914. sed 's/^X//' << 'SHAR_EOF' > 'libsipp/marble.c' &&
  915. /*
  916. X * marble shader - simulates marble using noise & turbulence
  917. X */
  918. X
  919. #include <math.h>
  920. #include <stdio.h>
  921. X
  922. #include "sipp.h"
  923. #include "noise.h"
  924. #include "shaders.h"
  925. X
  926. X
  927. extern bool noise_ready;
  928. X
  929. X
  930. static void
  931. marble(p, color, md)
  932. X    Vector *p;
  933. X    Color *color;
  934. X    Marble_desc *md;
  935. {
  936. X    double x, t;
  937. X
  938. X    x = p->x + turbulence(p, (double)(1.0 / 100.0)) * 5;
  939. X    x = sin(x);
  940. X    if (x > -0.1 && x < 0.1) {
  941. X        color->red = md->strip.red;
  942. X        color->grn = md->strip.grn;
  943. X        color->blu = md->strip.blu;
  944. X    } else if (x > -0.9 && x < 0.9){
  945. X        /* You are not supposed to understand this... */
  946. X        t = 7.0 / 6.0 - 1.0 / (6.25 * fabs(x) + 0.375);
  947. X        color->red = md->strip.red + t * (md->base.red - md->strip.red);
  948. X        color->grn = md->strip.grn + t * (md->base.grn - md->strip.grn);
  949. X        color->blu = md->strip.blu + t * (md->base.blu - md->strip.blu);
  950. X    } else {
  951. X        color->red = md->base.red;         
  952. X        color->grn = md->base.grn;
  953. X        color->blu = md->base.blu;
  954. X    }
  955. }
  956. X
  957. X
  958. X
  959. void
  960. marble_shader(nx, ny, nz, u, v, w, view_vec, lights, md, color)
  961. X    double  nx, ny, nz, u, v, w;
  962. X    Vector  view_vec;
  963. X    Lightsource *lights;
  964. X    Marble_desc *md;
  965. X    Color *color;
  966. {
  967. X    Vector     tmp;
  968. X    Surf_desc  surface;
  969. X
  970. X    if (!noise_ready) {
  971. X        noise_init();
  972. X    }
  973. X
  974. X    tmp.x = u * md->scale;
  975. X    tmp.y = v * md->scale;
  976. X    tmp.z = w * md->scale;
  977. X    marble(&tmp, &surface.color, md);
  978. X    surface.ambient  = md->ambient;
  979. X    surface.specular = md->specular;
  980. X    surface.c3       = md->c3;
  981. X    basic_shader(nx, ny, nz, u, v, w, view_vec, lights, &surface, color);
  982. }
  983. SHAR_EOF
  984. chmod 0644 libsipp/marble.c ||
  985. echo 'restore of libsipp/marble.c failed'
  986. Wc_c="`wc -c < 'libsipp/marble.c'`"
  987. test 1607 -eq "$Wc_c" ||
  988.     echo 'libsipp/marble.c: original size 1607, current size' "$Wc_c"
  989. fi
  990. # ============= libsipp/mask.c ==============
  991. if test -f 'libsipp/mask.c' -a X"$1" != X"-c"; then
  992.     echo 'x - skipping libsipp/mask.c (File already exists)'
  993. else
  994. echo 'x - extracting libsipp/mask.c (Text)'
  995. sed 's/^X//' << 'SHAR_EOF' > 'libsipp/mask.c' &&
  996. #include "sipp.h"
  997. #include "shaders.h"
  998. X
  999. void
  1000. mask_shader(a, b, c, u, v, w, view_vec, lights, sd, color)
  1001. X    double  a, b, c, u, v, w;
  1002. X    Vector  view_vec;
  1003. X    Lightsource *lights;
  1004. X    Mask_desc *sd;
  1005. X    Color *color;
  1006. {
  1007. X    int  ras_x, ras_y;
  1008. X    bool mapped;
  1009. X
  1010. X    ras_x = u * sd->xscale + sd->x0;
  1011. X    ras_y = v * sd->yscale + sd->y0;
  1012. X    mapped = ((ras_x >=0 && ras_x < sd->xsize) 
  1013. X              && (ras_y >= 0 && ras_y < sd->ysize)
  1014. X              && sd->pixel_test(sd->mask, ras_x, ras_y));
  1015. X    if (mapped) {
  1016. X        sd->fg_shader(a, b, c, u, v, w, view_vec, lights, sd->fg_surface,
  1017. X                      color); 
  1018. X    } else {
  1019. X        sd->bg_shader(a, b, c, u, v, w, view_vec, lights, sd->bg_surface,
  1020. X                      color); 
  1021. X    }
  1022. }
  1023. SHAR_EOF
  1024. chmod 0644 libsipp/mask.c ||
  1025. echo 'restore of libsipp/mask.c failed'
  1026. Wc_c="`wc -c < 'libsipp/mask.c'`"
  1027. test 741 -eq "$Wc_c" ||
  1028.     echo 'libsipp/mask.c: original size 741, current size' "$Wc_c"
  1029. fi
  1030. # ============= libsipp/noise.c ==============
  1031. if test -f 'libsipp/noise.c' -a X"$1" != X"-c"; then
  1032.     echo 'x - skipping libsipp/noise.c (File already exists)'
  1033. else
  1034. echo 'x - extracting libsipp/noise.c (Text)'
  1035. sed 's/^X//' << 'SHAR_EOF' > 'libsipp/noise.c' &&
  1036. #include <math.h>
  1037. X
  1038. #include "sipp.h"
  1039. #include "noise.h"
  1040. X
  1041. /* ================================================================ */
  1042. /*                      Noise and friends                           */
  1043. X
  1044. X
  1045. /*
  1046. X *    Noise and Dnoise routines
  1047. X *
  1048. X *    Many thanks to Jon Buller of Colorado State University for these
  1049. X *    routines.
  1050. X */
  1051. X
  1052. X
  1053. #define NUMPTS    512
  1054. #define P1    173
  1055. #define P2    263
  1056. #define P3    337
  1057. #define phi    0.6180339
  1058. X
  1059. X
  1060. bool noise_ready = FALSE;
  1061. X
  1062. static double pts[NUMPTS];
  1063. X
  1064. X
  1065. /*
  1066. X * Doubles might have values that is too large to be
  1067. X * stored in an int. If this is the case we "fold" the
  1068. X * value about MAXINT or MININT until it is of an
  1069. X * acceptable size.
  1070. X *
  1071. X * NOTE: 32 bit integers is assumed.
  1072. X */
  1073. static double
  1074. iadjust(f)
  1075. X    double f;
  1076. {
  1077. X    while ((f > 2147483647.0) || (f < -2147483648.0)) {
  1078. X        if (f > 2147483647.0) {         /* 2**31 - 1 */
  1079. X            f = (4294967294.0 - f);     /* 2**32 - 2 */
  1080. X        } else if (f < -2147483648.0){
  1081. X            f = (-4294967296.0 - f);
  1082. X        }
  1083. X    }
  1084. X    return f;
  1085. }
  1086. X
  1087. X
  1088. X
  1089. /*
  1090. X * Initialize the array of random numbers.
  1091. X * RANDOM() is defined in sipp.h
  1092. X */
  1093. void noise_init()
  1094. {
  1095. X    int i;
  1096. X    extern double drand48();
  1097. X   
  1098. X    for (i = 0; i < NUMPTS; ++i)
  1099. X        pts[i] = RANDOM();
  1100. X    noise_ready = TRUE;
  1101. }
  1102. X
  1103. X
  1104. X
  1105. double noise(v)
  1106. Vector *v;
  1107. {
  1108. X   Vector p;
  1109. X   int xi, yi, zi;
  1110. X   int xa, xb, xc, ya, yb, yc, za, zb, zc;
  1111. X   double xf, yf, zf;
  1112. X   double x2, x1, x0, y2, y1, y0, z2, z1, z0;
  1113. X   double p000, p100, p200, p010, p110, p210, p020, p120, p220;
  1114. X   double p001, p101, p201, p011, p111, p211, p021, p121, p221;
  1115. X   double p002, p102, p202, p012, p112, p212, p022, p122, p222;
  1116. X   double tmp;
  1117. X
  1118. X   p = *v;
  1119. X
  1120. X   p.x = iadjust(p.x);
  1121. X   p.y = iadjust(p.y);
  1122. X   p.z = iadjust(p.z);
  1123. X   xi = floor(p.x);
  1124. X   xa = floor(P1 * (xi * phi - floor(xi * phi)));
  1125. X   xb = floor(P1 * ((xi + 1) * phi - floor((xi + 1) * phi)));
  1126. X   xc = floor(P1 * ((xi + 2) * phi - floor((xi + 2) * phi)));
  1127. X
  1128. X   yi = floor(p.y);
  1129. X   ya = floor(P2 * (yi * phi - floor(yi * phi)));
  1130. X   yb = floor(P2 * ((yi + 1) * phi - floor((yi + 1) * phi)));
  1131. X   yc = floor(P2 * ((yi + 2) * phi - floor((yi + 2) * phi)));
  1132. X
  1133. X   zi = floor(p.z);
  1134. X   za = floor(P3 * (zi * phi - floor(zi * phi)));
  1135. X   zb = floor(P3 * ((zi + 1) * phi - floor((zi + 1) * phi)));
  1136. X   zc = floor(P3 * ((zi + 2) * phi - floor((zi + 2) * phi)));
  1137. X
  1138. X   p000 = pts[(xa + ya + za) % NUMPTS];
  1139. X   p100 = pts[(xb + ya + za) % NUMPTS];
  1140. X   p200 = pts[(xc + ya + za) % NUMPTS];
  1141. X   p010 = pts[(xa + yb + za) % NUMPTS];
  1142. X   p110 = pts[(xb + yb + za) % NUMPTS];
  1143. X   p210 = pts[(xc + yb + za) % NUMPTS];
  1144. X   p020 = pts[(xa + yc + za) % NUMPTS];
  1145. X   p120 = pts[(xb + yc + za) % NUMPTS];
  1146. X   p220 = pts[(xc + yc + za) % NUMPTS];
  1147. X   p001 = pts[(xa + ya + zb) % NUMPTS];
  1148. X   p101 = pts[(xb + ya + zb) % NUMPTS];
  1149. X   p201 = pts[(xc + ya + zb) % NUMPTS];
  1150. X   p011 = pts[(xa + yb + zb) % NUMPTS];
  1151. X   p111 = pts[(xb + yb + zb) % NUMPTS];
  1152. X   p211 = pts[(xc + yb + zb) % NUMPTS];
  1153. X   p021 = pts[(xa + yc + zb) % NUMPTS];
  1154. X   p121 = pts[(xb + yc + zb) % NUMPTS];
  1155. X   p221 = pts[(xc + yc + zb) % NUMPTS];
  1156. X   p002 = pts[(xa + ya + zc) % NUMPTS];
  1157. X   p102 = pts[(xb + ya + zc) % NUMPTS];
  1158. X   p202 = pts[(xc + ya + zc) % NUMPTS];
  1159. X   p012 = pts[(xa + yb + zc) % NUMPTS];
  1160. X   p112 = pts[(xb + yb + zc) % NUMPTS];
  1161. X   p212 = pts[(xc + yb + zc) % NUMPTS];
  1162. X   p022 = pts[(xa + yc + zc) % NUMPTS];
  1163. X   p122 = pts[(xb + yc + zc) % NUMPTS];
  1164. X   p222 = pts[(xc + yc + zc) % NUMPTS];
  1165. X
  1166. X   xf = p.x - xi;
  1167. X   x1 = xf * xf;
  1168. X   x2 = 0.5 * x1;
  1169. X   x1 = 0.5 + xf - x1;
  1170. X   x0 = 0.5 - xf + x2;
  1171. X
  1172. X   yf = p.y - yi;
  1173. X   y1 = yf * yf;
  1174. X   y2 = 0.5 * y1;
  1175. X   y1 = 0.5 + yf - y1;
  1176. X   y0 = 0.5 - yf + y2;
  1177. X
  1178. X   zf = p.z - zi;
  1179. X   z1 = zf * zf;
  1180. X   z2 = 0.5 * z1;
  1181. X   z1 = 0.5 + zf - z1;
  1182. X   z0 = 0.5 - zf + z2;
  1183. X
  1184. X   /*
  1185. X    * This expression is split up since some compilers
  1186. X    * chokes on expressions of this size.
  1187. X    */
  1188. X   tmp =  z0 * (y0 * (x0 * p000 + x1 * p100 + x2 * p200) +
  1189. X                y1 * (x0 * p010 + x1 * p110 + x2 * p210) +
  1190. X                y2 * (x0 * p020 + x1 * p120 + x2 * p220));
  1191. X   tmp += z1 * (y0 * (x0 * p001 + x1 * p101 + x2 * p201) +
  1192. X                y1 * (x0 * p011 + x1 * p111 + x2 * p211) +
  1193. X                y2 * (x0 * p021 + x1 * p121 + x2 * p221));
  1194. X   tmp += z2 * (y0 * (x0 * p002 + x1 * p102 + x2 * p202) +
  1195. X                y1 * (x0 * p012 + x1 * p112 + x2 * p212) +
  1196. X                y2 * (x0 * p022 + x1 * p122 + x2 * p222));
  1197. X
  1198. X   return tmp;
  1199. }
  1200. X
  1201. X
  1202. X
  1203. double turbulence(p, size)
  1204. X    Vector *p;
  1205. X    double size;
  1206. {
  1207. X    Vector tmp;
  1208. X    double scale = 1.0,
  1209. X           t     = 0.0;
  1210. X
  1211. X    while (scale > size) {
  1212. X        tmp.x = p->x / scale;
  1213. X        tmp.y = p->y / scale;
  1214. X        tmp.z = p->z / scale;
  1215. X        t += fabs(noise(&tmp) * scale);
  1216. X        scale /= 2.0;
  1217. X    }
  1218. X    return t;
  1219. }
  1220. X
  1221. X
  1222. X
  1223. Vector Dnoise(p)
  1224. Vector *p;
  1225. {
  1226. X   Vector v;
  1227. X   int xi, yi, zi;
  1228. X   int xa, xb, xc, ya, yb, yc, za, zb, zc;
  1229. X   double xf, yf, zf;
  1230. X   double x2, x1, x0, y2, y1, y0, z2, z1, z0;
  1231. X   double xd2, xd1, xd0, yd2, yd1, yd0, zd2, zd1, zd0;
  1232. X   double p000, p100, p200, p010, p110, p210, p020, p120, p220;
  1233. X   double p001, p101, p201, p011, p111, p211, p021, p121, p221;
  1234. X   double p002, p102, p202, p012, p112, p212, p022, p122, p222;
  1235. X
  1236. X   xi = floor(p->x);
  1237. X   xa = floor(P1 * (xi * phi - floor(xi * phi)));
  1238. X   xb = floor(P1 * ((xi + 1) * phi - floor((xi + 1) * phi)));
  1239. X   xc = floor(P1 * ((xi + 2) * phi - floor((xi + 2) * phi)));
  1240. X
  1241. X   yi = floor(p->y);
  1242. X   ya = floor(P2 * (yi * phi - floor(yi * phi)));
  1243. X   yb = floor(P2 * ((yi + 1) * phi - floor((yi + 1) * phi)));
  1244. X   yc = floor(P2 * ((yi + 2) * phi - floor((yi + 2) * phi)));
  1245. X
  1246. X   zi = floor(p->z);
  1247. X   za = floor(P3 * (zi * phi - floor(zi * phi)));
  1248. X   zb = floor(P3 * ((zi + 1) * phi - floor((zi + 1) * phi)));
  1249. X   zc = floor(P3 * ((zi + 2) * phi - floor((zi + 2) * phi)));
  1250. X
  1251. X   p000 = pts[(xa + ya + za) % NUMPTS];
  1252. X   p100 = pts[(xb + ya + za) % NUMPTS];
  1253. X   p200 = pts[(xc + ya + za) % NUMPTS];
  1254. X   p010 = pts[(xa + yb + za) % NUMPTS];
  1255. X   p110 = pts[(xb + yb + za) % NUMPTS];
  1256. X   p210 = pts[(xc + yb + za) % NUMPTS];
  1257. X   p020 = pts[(xa + yc + za) % NUMPTS];
  1258. X   p120 = pts[(xb + yc + za) % NUMPTS];
  1259. X   p220 = pts[(xc + yc + za) % NUMPTS];
  1260. X   p001 = pts[(xa + ya + zb) % NUMPTS];
  1261. X   p101 = pts[(xb + ya + zb) % NUMPTS];
  1262. X   p201 = pts[(xc + ya + zb) % NUMPTS];
  1263. X   p011 = pts[(xa + yb + zb) % NUMPTS];
  1264. X   p111 = pts[(xb + yb + zb) % NUMPTS];
  1265. X   p211 = pts[(xc + yb + zb) % NUMPTS];
  1266. X   p021 = pts[(xa + yc + zb) % NUMPTS];
  1267. X   p121 = pts[(xb + yc + zb) % NUMPTS];
  1268. X   p221 = pts[(xc + yc + zb) % NUMPTS];
  1269. X   p002 = pts[(xa + ya + zc) % NUMPTS];
  1270. X   p102 = pts[(xb + ya + zc) % NUMPTS];
  1271. X   p202 = pts[(xc + ya + zc) % NUMPTS];
  1272. X   p012 = pts[(xa + yb + zc) % NUMPTS];
  1273. X   p112 = pts[(xb + yb + zc) % NUMPTS];
  1274. X   p212 = pts[(xc + yb + zc) % NUMPTS];
  1275. X   p022 = pts[(xa + yc + zc) % NUMPTS];
  1276. X   p122 = pts[(xb + yc + zc) % NUMPTS];
  1277. X   p222 = pts[(xc + yc + zc) % NUMPTS];
  1278. X
  1279. X   xf = p->x - xi;
  1280. X   x1 = xf * xf;
  1281. X   x2 = 0.5 * x1;
  1282. X   x1 = 0.5 + xf - x1;
  1283. X   x0 = 0.5 - xf + x2;
  1284. X   xd2 = xf;
  1285. X   xd1 = 1.0 - xf - xf;
  1286. X   xd0 = xf - 1.0;
  1287. X
  1288. X   yf = p->y - yi;
  1289. X   y1 = yf * yf;
  1290. X   y2 = 0.5 * y1;
  1291. X   y1 = 0.5 + yf - y1;
  1292. X   y0 = 0.5 - yf + y2;
  1293. X   yd2 = yf;
  1294. X   yd1 = 1.0 - yf - yf;
  1295. X   yd0 = yf - 1.0;
  1296. X
  1297. X   zf = p->z - zi;
  1298. X   z1 = zf * zf;
  1299. X   z2 = 0.5 * z1;
  1300. X   z1 = 0.5 + zf - z1;
  1301. X   z0 = 0.5 - zf + z2;
  1302. X   zd2 = zf;
  1303. X   zd1 = 1.0 - zf - zf;
  1304. X   zd0 = zf - 1.0;
  1305. X
  1306. X   /*
  1307. X    * This expressions are split up since some compilers
  1308. X    * chokes on expressions of this size.
  1309. X    */
  1310. X   v.x        = z0 * (y0 * (xd0 * p000 + xd1 * p100 + xd2 * p200) +
  1311. X                      y1 * (xd0 * p010 + xd1 * p110 + xd2 * p210) +
  1312. X                      y2 * (xd0 * p020 + xd1 * p120 + xd2 * p220));
  1313. X   v.x       += z1 * (y0 * (xd0 * p001 + xd1 * p101 + xd2 * p201) +
  1314. X                      y1 * (xd0 * p011 + xd1 * p111 + xd2 * p211) +
  1315. X                      y2 * (xd0 * p021 + xd1 * p121 + xd2 * p221));
  1316. X   v.x       += z2 * (y0 * (xd0 * p002 + xd1 * p102 + xd2 * p202) +
  1317. X                      y1 * (xd0 * p012 + xd1 * p112 + xd2 * p212) +
  1318. X                      y2 * (xd0 * p022 + xd1 * p122 + xd2 * p222));
  1319. X                                  
  1320. X   v.y        = z0 * (yd0 * (x0 * p000 + x1 * p100 + x2 * p200) +
  1321. X                      yd1 * (x0 * p010 + x1 * p110 + x2 * p210) +
  1322. X                      yd2 * (x0 * p020 + x1 * p120 + x2 * p220));
  1323. X   v.y       += z1 * (yd0 * (x0 * p001 + x1 * p101 + x2 * p201) +
  1324. X                      yd1 * (x0 * p011 + x1 * p111 + x2 * p211) +
  1325. X                      yd2 * (x0 * p021 + x1 * p121 + x2 * p221));
  1326. X   v.y       += z2 * (yd0 * (x0 * p002 + x1 * p102 + x2 * p202) +
  1327. X                      yd1 * (x0 * p012 + x1 * p112 + x2 * p212) +
  1328. X                      yd2 * (x0 * p022 + x1 * p122 + x2 * p222));
  1329. X                                  
  1330. X   v.z        = zd0 * (y0 * (x0 * p000 + x1 * p100 + x2 * p200) +
  1331. X                       y1 * (x0 * p010 + x1 * p110 + x2 * p210) +
  1332. X                       y2 * (x0 * p020 + x1 * p120 + x2 * p220));
  1333. X   v.z       += zd1 * (y0 * (x0 * p001 + x1 * p101 + x2 * p201) +
  1334. X                       y1 * (x0 * p011 + x1 * p111 + x2 * p211) +
  1335. X                       y2 * (x0 * p021 + x1 * p121 + x2 * p221));
  1336. X   v.z       += zd2 * (y0 * (x0 * p002 + x1 * p102 + x2 * p202) +
  1337. X                       y1 * (x0 * p012 + x1 * p112 + x2 * p212) +
  1338. X                       y2 * (x0 * p022 + x1 * p122 + x2 * p222));
  1339. X
  1340. X   return v;
  1341. }
  1342. X
  1343. X
  1344. X
  1345. /*
  1346. X * End of noise routines
  1347. X */
  1348. SHAR_EOF
  1349. chmod 0644 libsipp/noise.c ||
  1350. echo 'restore of libsipp/noise.c failed'
  1351. Wc_c="`wc -c < 'libsipp/noise.c'`"
  1352. test 9261 -eq "$Wc_c" ||
  1353.     echo 'libsipp/noise.c: original size 9261, current size' "$Wc_c"
  1354. fi
  1355. # ============= libsipp/noise.h ==============
  1356. if test -f 'libsipp/noise.h' -a X"$1" != X"-c"; then
  1357.     echo 'x - skipping libsipp/noise.h (File already exists)'
  1358. else
  1359. echo 'x - extracting libsipp/noise.h (Text)'
  1360. sed 's/^X//' << 'SHAR_EOF' > 'libsipp/noise.h' &&
  1361. /*
  1362. X * Declarations needed to use noise() and friends...
  1363. X */
  1364. X
  1365. #ifndef _NOISE_H 
  1366. #define _NOISE_H 
  1367. X
  1368. X
  1369. extern void     noise_init();
  1370. extern double   noise();
  1371. extern double   turbulence();
  1372. extern Vector   Dnoise();
  1373. X
  1374. X
  1375. #endif /* _NOISE_H */
  1376. SHAR_EOF
  1377. chmod 0644 libsipp/noise.h ||
  1378. echo 'restore of libsipp/noise.h failed'
  1379. Wc_c="`wc -c < 'libsipp/noise.h'`"
  1380. test 234 -eq "$Wc_c" ||
  1381.     echo 'libsipp/noise.h: original size 234, current size' "$Wc_c"
  1382. fi
  1383. # ============= libsipp/planet.c ==============
  1384. if test -f 'libsipp/planet.c' -a X"$1" != X"-c"; then
  1385.     echo 'x - skipping libsipp/planet.c (File already exists)'
  1386. else
  1387. echo 'x - extracting libsipp/planet.c (Text)'
  1388. sed 's/^X//' << 'SHAR_EOF' > 'libsipp/planet.c' &&
  1389. #include <stdio.h>
  1390. #include <math.h>
  1391. X
  1392. #include "sipp.h"
  1393. #include "geometric.h"
  1394. #include "noise.h"
  1395. X
  1396. X
  1397. /* A reasonably nice brown color */
  1398. static Color  land = {0.28125, 0.1875, 0.09375};
  1399. X
  1400. /* Oceans are usually blue */
  1401. static Color  sea = {0.0, 0.0, 1.0};
  1402. X
  1403. /* And Clouds are white */
  1404. static Color  cloud = {1.0, 1.0, 1.0};
  1405. X
  1406. X
  1407. /* 
  1408. X * This was designed to work with a unit sphere.  
  1409. X * 
  1410. X * Thanks to Jon Buller       jonb@vector.dallas.tx.us
  1411. X */
  1412. double
  1413. turb(size, scale_factor, loc)
  1414. X    int size;
  1415. X    double scale_factor;
  1416. X    Vector loc;
  1417. {
  1418. X    double cur_scale, result;
  1419. X    int cur;
  1420. X
  1421. X    result = noise(&loc);
  1422. X    cur_scale = 1.0;
  1423. X
  1424. X    cur = 1;
  1425. X    while (cur < size) {
  1426. X        cur <<= 1;
  1427. X        cur_scale = cur_scale * scale_factor;
  1428. X        loc.x *= 2.0;
  1429. X        loc.y *= 2.0;
  1430. X        loc.z *= 2.0;
  1431. X        result += noise(&loc) * cur_scale;
  1432. X    }
  1433. X    return result;
  1434. }
  1435. X
  1436. X
  1437. X
  1438. extern bool noise_ready;
  1439. X
  1440. void
  1441. planet_shader(nx, ny, nz, u, v, w, view_vec, lights, sd, color)
  1442. X    double        nx, ny, nz, u, v, w;
  1443. X    Vector        view_vec;
  1444. X    Lightsource  *lights;
  1445. X    Surf_desc    *sd;
  1446. X    Color        *color;
  1447. {
  1448. X    Vector  tmp;
  1449. X    double  amt;
  1450. X
  1451. X    if (!noise_ready) {
  1452. X        noise_init();
  1453. X    }
  1454. X
  1455. X    tmp.x = u;
  1456. X    tmp.y = v;
  1457. X    tmp.z = w;
  1458. X
  1459. X    if (turb(430, 0.7, tmp) > 0.15)
  1460. X        sd->color = land;
  1461. X    else 
  1462. X        sd->color = sea;
  1463. X
  1464. X    VecScalMul(tmp, 12.0, tmp)
  1465. X
  1466. X    amt = turb(18, 0.6, tmp);
  1467. X    if (amt > -0.25) {
  1468. X        amt += 0.25;
  1469. X        sd->color.red += amt * (cloud.red - sd->color.red);
  1470. X        sd->color.grn += amt * (cloud.grn - sd->color.grn);
  1471. X        sd->color.blu += amt * (cloud.blu - sd->color.blu);
  1472. X    }
  1473. X
  1474. X    basic_shader(nx, ny, nz, u, v, w, view_vec, lights, sd, color);
  1475. }
  1476. SHAR_EOF
  1477. chmod 0644 libsipp/planet.c ||
  1478. echo 'restore of libsipp/planet.c failed'
  1479. Wc_c="`wc -c < 'libsipp/planet.c'`"
  1480. test 1700 -eq "$Wc_c" ||
  1481.     echo 'libsipp/planet.c: original size 1700, current size' "$Wc_c"
  1482. fi
  1483. # ============= libsipp/primitives.h ==============
  1484. if test -f 'libsipp/primitives.h' -a X"$1" != X"-c"; then
  1485.     echo 'x - skipping libsipp/primitives.h (File already exists)'
  1486. else
  1487. echo 'x - extracting libsipp/primitives.h (Text)'
  1488. sed 's/^X//' << 'SHAR_EOF' > 'libsipp/primitives.h' &&
  1489. extern Object *sipp_torus();
  1490. extern Object *sipp_cylinder();
  1491. extern Object *sipp_ellipsoid();
  1492. extern Object *sipp_sphere();
  1493. extern Object *sipp_block();
  1494. extern Object *sipp_cube();
  1495. extern Object *sipp_bezier();
  1496. SHAR_EOF
  1497. chmod 0644 libsipp/primitives.h ||
  1498. echo 'restore of libsipp/primitives.h failed'
  1499. Wc_c="`wc -c < 'libsipp/primitives.h'`"
  1500. test 211 -eq "$Wc_c" ||
  1501.     echo 'libsipp/primitives.h: original size 211, current size' "$Wc_c"
  1502. fi
  1503. # ============= libsipp/shaders.h ==============
  1504. if test -f 'libsipp/shaders.h' -a X"$1" != X"-c"; then
  1505.     echo 'x - skipping libsipp/shaders.h (File already exists)'
  1506. else
  1507. echo 'x - extracting libsipp/shaders.h (Text)'
  1508. sed 's/^X//' << 'SHAR_EOF' > 'libsipp/shaders.h' &&
  1509. /*
  1510. X * This include file defines the different shaders availiable in
  1511. X * sipp. Each shader is defined by a structure containing the necessary
  1512. X * parameters to describe how a surface should be shaded with that 
  1513. X * particular shader, and the extern declaration of the shader function 
  1514. X * itself.
  1515. X */
  1516. X
  1517. X
  1518. #ifndef _SHADERS_H
  1519. #define _SHADERS_H
  1520. X
  1521. X
  1522. #include <sipp.h>
  1523. X
  1524. X
  1525. X
  1526. /* 
  1527. X * Surface description for the bozo shader.
  1528. X */
  1529. typedef struct {
  1530. X    Color   *colors;
  1531. X    int      no_of_cols;
  1532. X    double   ambient;
  1533. X    double   specular;
  1534. X    double   c3;
  1535. X    double   scale;        /* Scale the texture by this value */
  1536. } Bozo_desc;
  1537. X    
  1538. X
  1539. X
  1540. /*
  1541. X * Surface description used by the marble shader. marble_shader
  1542. X * creates a solid texture (using noise & turbulence) that
  1543. X * simulates marble.
  1544. X */
  1545. typedef struct {
  1546. X    double   ambient;
  1547. X    double   specular;
  1548. X    double   c3;
  1549. X    double   scale;        /* Scale the marble texture by this value */
  1550. X    Color    base;         /* "Base" color of the surface */
  1551. X    Color    strip;        /* Color of the "stripes" in the marble */
  1552. } Marble_desc;
  1553. X
  1554. X
  1555. X
  1556. /*
  1557. X * Surface description used by the granite shader. granite_shader
  1558. X * creates a solid texture (using noise) that mixes two colors
  1559. X * to simulate granite.
  1560. X */
  1561. typedef struct {
  1562. X    double   ambient;
  1563. X    double   specular;
  1564. X    double   c3;
  1565. X    double   scale;        /* Scale the texture by this value */
  1566. X    Color    col1;         /* The two color components */
  1567. X    Color    col2;         /*                          */
  1568. } Granite_desc;
  1569. X
  1570. X
  1571. X
  1572. /*
  1573. X * Mask shader. This shader is SUN specific since it uses
  1574. X * a pixrect. It projects the pixrect on the x-y plane (in
  1575. X * texture coordinates). When a surface is shaded it checks
  1576. X * if the x, y coordinate in the pixrect is zero and calls
  1577. X * one of two other shaders depending of the outcome of that 
  1578. X * test.
  1579. X */
  1580. typedef struct {
  1581. X    Shader *fg_shader;          /* Shader to call if mask(x, y) != 0 */
  1582. X    void   *fg_surface;         /* Surface description for fg_shader */
  1583. X    Shader *bg_shader;          /* Shader to call if mask(x, y) == 0 */
  1584. X    void   *bg_surface;         /* Surface description for bg_shader */
  1585. X    void   *mask;               /* Pointer to mask image */
  1586. X    bool  (*pixel_test)();      /* Function that tests a pixel value */
  1587. X    int     x0, y0;             /* Where to put origo in the mask image */
  1588. X    int     xsize, ysize;       /* Size of the mask image */
  1589. X    double  xscale, yscale;     /* Scale the mask image with these values */
  1590. } Mask_desc;
  1591. X
  1592. X
  1593. /*
  1594. X * Surface description for the bumpy_shader(). This shader 
  1595. X * fiddles with the surface normals of a surface so the surface 
  1596. X * looks bumpy.
  1597. X */
  1598. X
  1599. typedef struct {
  1600. X    Shader *shader;
  1601. X    void   *surface;
  1602. X    double scale;
  1603. X    bool   bumpflag;
  1604. X    bool   holeflag;
  1605. } Bumpy_desc;
  1606. X
  1607. X
  1608. /*
  1609. X * Declarations of the actual shading functions.
  1610. X */
  1611. extern void marble_shader();
  1612. extern void granite_shader();
  1613. extern void bozo_shader();
  1614. extern void mask_shader();
  1615. extern void bumpy_shader();
  1616. extern void planet_shader();
  1617. X
  1618. #endif /* _SHADERS_H */
  1619. SHAR_EOF
  1620. chmod 0644 libsipp/shaders.h ||
  1621. echo 'restore of libsipp/shaders.h failed'
  1622. Wc_c="`wc -c < 'libsipp/shaders.h'`"
  1623. test 3024 -eq "$Wc_c" ||
  1624.     echo 'libsipp/shaders.h: original size 3024, current size' "$Wc_c"
  1625. fi
  1626. true || echo 'restore of libsipp/sipp.c failed'
  1627. echo End of part 2, continue with part 3
  1628. exit 0
  1629.  
  1630. -- 
  1631. Inge Wallin               | Thus spake the master programmer:               |
  1632.                           |      "After three days without programming,     |
  1633. ingwa@isy.liu.se          |       life becomes meaningless."                |
  1634.                           | Geoffrey James: The Tao of Programming.         |
  1635.  
  1636.  
  1637. exit 0 # Just in case...
  1638. -- 
  1639. Kent Landfield                   INTERNET: kent@sparky.IMD.Sterling.COM
  1640. Sterling Software, IMD           UUCP:     uunet!sparky!kent
  1641. Phone:    (402) 291-8300         FAX:      (402) 291-4362
  1642. Please send comp.sources.misc-related mail to kent@uunet.uu.net.
  1643.